package com.lqm.web.controller;


import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @program: Spring_Boot
 * @description
 * @author: lqm
 * @create: 2018-11-26 15:30
 */
@RestController
@RequestMapping("/api")
public class UploadController {
    @RequestMapping("/upload")
    public Map<String,Object> upload(@RequestParam("file") MultipartFile files, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try{
            //设定上传的基本路径
            String basepath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/upload/";
            String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            File f = new File(basepath, datePath);
            if (!f.exists()) {
                f.mkdirs();
            }
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filename = uuid+files.getOriginalFilename();
            try {
                files.transferTo(new File(f, filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
            map.put("code",0);
            map.put("msg","上传成功");
            map.put("src","/upload" + "/" + datePath + "/" + filename);

        }catch (Exception e){
            e.printStackTrace();
            map.put("code",101);
            map.put("msg","上传失败");
            map.put("src","");
        }
        System.out.println("UploadController:"+map);
        return map;
    }



    @RequestMapping("/edit_upload")
    public Map<String,Object> editUpload(@RequestParam("imgFile") MultipartFile files, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try{
            //设定上传的基本路径
            String basepath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/upload/";
            int c;
            String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            File f = new File(basepath, datePath);
            int b;
            if (!f.exists()) {
                f.mkdirs();
            }
            int a;
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String filename = uuid+files.getOriginalFilename();
            try {
                files.transferTo(new File(f, filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
            map.put("error",0);
            map.put("msg","上传成功");
            map.put("url","/upload" + "/" + datePath + "/" + filename);

        }catch (Exception e){
            e.printStackTrace();
            map.put("error",1);
            map.put("url","");
        }
        System.out.println("UploadController:"+map);
        return map;
    }

}
